package com.stu.widget;

import com.stu.aq.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class EntryButton extends View {
	private Paint buttonPaint, textPaint, linePaint;
	private Rect buttonRect;
	private String buttonText;
	private final static int padding = 10;
	private final static int width = 90;
	private final static float startX = 40;
	private final static float endX = 50;
	private final static int entryWidth = 100;
	private final static int height = 60;
	private final static int textYOffset = 5;
	private boolean isNumber, isDone;
	private Bitmap del, delDis;
	
	public enum State { NORMAL, PRESSED, DISABLED }
	private State state;

	public EntryButton( Context c ) {
		super( c );
		init( c );
	}
	
	public EntryButton(Context context, AttributeSet attrs) {
		super( context, attrs );
		init( context );
	}

	private void init( Context c ) {
		buttonPaint = new Paint();
		buttonPaint.setColor( Color.LTGRAY );
		textPaint = new Paint();
		textPaint.setColor( Color.BLACK );
		textPaint.setTextSize( 14 );
		textPaint.setTextAlign( Paint.Align.CENTER );
		linePaint = new Paint();
		linePaint.setColor( Color.BLACK );
		linePaint.setStrokeWidth( 2 );
		buttonRect = new Rect( padding, padding, width-padding, height-padding );
		state = State.NORMAL;
		isDone = false;
	}

	public void setText( String s ) {
		buttonText = s;
		isNumber = true;
		state = State.NORMAL;
		isDone = false;
	}

	public void setCancelButton() {
		buttonText = "Cancel";
		buttonRect = new Rect( padding, padding, entryWidth-padding, height-padding );
		isNumber = false;
		state = State.NORMAL;
		isDone = false;
	}
	
	public void setDoneButton() {
		buttonText = "Done";
		buttonRect = new Rect( padding, padding, entryWidth-padding, height-padding );
		buttonPaint.setColor( Color.GREEN );
		isNumber = false;
		state = State.NORMAL;
		isDone = true;
	}

	public void setDeleteButton( Context c ) {
		del = BitmapFactory.decodeResource( c.getResources(), R.drawable.delete );
		delDis = BitmapFactory.decodeResource( c.getResources(), R.drawable.delete_disabled );
		buttonText = "Del";
		buttonRect = new Rect( padding, padding, entryWidth-padding, height-padding );
		isNumber = false;
		state = State.NORMAL;
		isDone = false;
	}

	public State getState() {
		return state;
	}
	
	public void setState( State s ) {
		if( state != s ) {
			if( s == State.NORMAL ) {
				// enabled
				if( isDone )
					buttonPaint.setColor( Color.GREEN );
				else
					buttonPaint.setColor( Color.LTGRAY );
				textPaint.setColor( Color.BLACK );
				linePaint.setColor( Color.BLACK );
			} else if( s == State.DISABLED ){
				// disabled
				buttonPaint.setColor( Color.rgb(64, 64, 64) );
				textPaint.setColor( Color.rgb(128, 128, 128) );
				linePaint.setColor( Color.rgb(128, 128, 128) );
			} else {
				// pressed
				buttonPaint.setColor( Color.argb( 32, 0, 0, 255 ) );
			}
			state = s;
			invalidate();
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if( isNumber )
			setMeasuredDimension(width, height);
		else
			setMeasuredDimension(entryWidth, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if( del != null ) {
			if( state == State.NORMAL )
				canvas.drawBitmap( del, padding, padding, buttonPaint );
			else if( state == State.DISABLED )
				canvas.drawBitmap( delDis, padding, padding, buttonPaint );
			else {
				// PRESSED
				canvas.drawBitmap( del, padding, padding, buttonPaint );
				canvas.drawRect( buttonRect, buttonPaint );
			}
		} else {
			canvas.drawRect( buttonRect, buttonPaint );
			if( buttonText != null ) {
				if( buttonText.charAt(0) == '.' )
					canvas.drawCircle( width/2, height/2, 2, textPaint );
				else if( buttonText.charAt(0) == '-' )
					canvas.drawLine( startX, height/2, endX, height/2, linePaint );
				else {
					if( isNumber )
						canvas.drawText( buttonText, width/2, (height/2)+textYOffset, textPaint );
					else
						canvas.drawText( buttonText, entryWidth/2, (height/2)+textYOffset, textPaint );
				}
			}
		}
	}

}
